package practice.phase03.module01.mapreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 1. 获取配置，获取Job(任务)实例
        final Configuration conf = new Configuration();
        Job wordCount = Job.getInstance(conf, "WordCount");

        // 2. 设置driver类
        wordCount.setJarByClass(WordCountDriver.class);

        // 3. 设置Map,Reduce类
        wordCount.setMapperClass(WordCountMapper.class);
        wordCount.setReducerClass(WordCountReducer.class);

        // 4. 设置Map输出KEY-VALUE类型
        wordCount.setMapOutputKeyClass(Text.class);
        wordCount.setMapOutputValueClass(IntWritable.class);

        // 5. 设置Reduce输出KEY-VALUE类型
        wordCount.setOutputKeyClass(Text.class);
        wordCount.setMapOutputValueClass(IntWritable.class);

        // 6. 设置输入，输出路径
        FileInputFormat.setInputPaths(wordCount, new Path(args[0]));
        FileOutputFormat.setOutputPath(wordCount, new Path(args[1]));

        // 7. 提交任务
        boolean b = wordCount.waitForCompletion(true);

        // 8. 处理返回结果
        System.exit(b ? 0 : 1);
    }
}
